﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
using System.Xml.Serialization;
using System.Web.Services;
using System.Web.Services.Protocols;
using AirLine.Entities2;
using AirLine.DataAccess;
using System.IO;
using System.Xml;

/// <summary>
/// Summary description for AirLineService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class AirLineService : System.Web.Services.WebService {

        #region Initialization

        private IRepository repository;

        public AirLineService()
        {            
            repository = new Repository(new AirLineEntities());
        }

        public AirLineService(IRepository repository)
        {
            this.repository = repository;
        }


        #endregion
    
    [WebMethod]
    public void TestException(DateTime TestDatetime)
    {
        
    }
    [WebMethod(EnableSession = true)]
    public bool DangNhap(string tenTaiKhoan, string matKhau)
    {
        DateTime toDay = DateTime.Today;
        TaiKhoanWebService taiKhoan = repository.Get<TaiKhoanWebService>(x => x.TenTaiKhoan == tenTaiKhoan && x.MatKhau == matKhau && x.NgayBatDauSD <= toDay && x.NgayKetThuc >= toDay).SingleOrDefault();
        if (taiKhoan != null)
        {
            Session.Add("TenTaiKhoan", tenTaiKhoan);
            Session.Add("IsLogin", true);
            return true;
        }
        return false;
    }

    [WebMethod(EnableSession = true)]
    public string KiemTraDangNhap()
    {
        if (Session["isLogin"] != null && (bool)Session["isLogin"])
            return (string)Session["TenTaiKhoan"];
        return "Chua Dang Nhap";
    }

    [WebMethod(EnableSession = true)]
    public List<ChuyenBay> TimChuyenBay(string sanBayDi, string sanBayDen, string thoiGianDi, string thoiGianVe, string loaiVe)
    {
        if (Session["isLogin"] != null && (bool)Session["isLogin"])
        {
            var ThoiGianDi = DateTime.Parse(thoiGianDi);
            List<ChuyenBay> ChuyenBays = repository.Get<ChuyenBay>(x => x.SanBayDi == sanBayDi && x.SanBayDen == sanBayDen).ToList() ;
            ChuyenBays = ChuyenBays.Where(x => x.ThoiGianDi.Date == ThoiGianDi.Date).ToList() ;
            if ((loaiVe.ToLower() == "khu hoi" || loaiVe.ToLower() == "khứ hồi") && thoiGianVe != null)
            {
                var ThoiGianVe = DateTime.Parse(thoiGianVe);
                List<ChuyenBay> ChuyenBayVes =repository.Get<ChuyenBay>(x => x.SanBayDi == sanBayDen && x.SanBayDen == sanBayDi).ToList();
                ChuyenBays.AddRange(ChuyenBayVes.Where(x => x.ThoiGianDi.Date == ThoiGianVe.Date).ToList());
            }
            return ChuyenBays;
        }
        return null;
    }

    [WebMethod(EnableSession = true)]
    public List<SanBay> TimSanBay(string quocGia)
    {
        if (Session["isLogin"] != null && (bool)Session["isLogin"])
        {
            return repository.Get<SanBay>(sanbay => sanbay.QuocGia == quocGia).ToList();
        }
        return null;
    }

    [WebMethod(EnableSession = true)]
    public List<LoaiVe> DanhSachLoaiVe()
    {
        if (Session["isLogin"] != null && (bool)Session["isLogin"])
        {
            return repository.GetAll<LoaiVe>().ToList<LoaiVe>();
        }
            return null;
    }

    [WebMethod(EnableSession = true)]
    public List<LoaiKhachHang> DanhSachLoaiKhachHang()
    {
        if (Session["isLogin"] != null && (bool)Session["isLogin"])
        {
            return repository.GetAll<LoaiKhachHang>().ToList<LoaiKhachHang>();
        }
        return null;
    }

    [WebMethod(EnableSession = true)]
    public List<HangGhe> DanhSachHangGhe()
    {
        if (Session["isLogin"] != null && (bool)Session["isLogin"])
        {
            return repository.GetAll<HangGhe>().ToList<HangGhe>();
        }
        return null;
    }

    [WebMethod(EnableSession = true) ]
    public double TimGiaVe(string maHangVe, string maSanBayDi, string maSanBayDen, string loaiChuyenBay, string thoiGianDi, string thoiGianVe)
    {
        if (Session["isLogin"] != null && (bool)Session["isLogin"])
        {
            var ThoiGianDi = DateTime.Parse(thoiGianDi);
            
            double gia = 0;
            int HangVe = Convert.ToInt32(maHangVe);
            HangGhe hangGhe = repository.Get<HangGhe>(x => x.MaHangGhe == HangVe).SingleOrDefault();
            List<ChuyenBay> chuyenBayDis = repository.Get<ChuyenBay>(x => x.SanBayDi == maSanBayDi
                                                                        && x.SanBayDen == maSanBayDen).ToList();
            ChuyenBay chuyenBay = chuyenBayDis.Where(cb => cb.ThoiGianDi.Date == ThoiGianDi.Date).FirstOrDefault();

            if (chuyenBay == null) return gia;

            gia = (double)chuyenBay.Gia + (double)hangGhe.Gia;

            if (loaiChuyenBay.ToLower() == "khu hoi" || loaiChuyenBay.ToLower() == "khứ hồi")
            {
                var ThoiGianVe = DateTime.Parse(thoiGianVe);
                List<ChuyenBay> chuyenBayVes = repository.Get<ChuyenBay>(x => x.SanBayDi == maSanBayDen
                                                                        && x.SanBayDen == maSanBayDi).ToList();
                chuyenBay = chuyenBayVes.Where(cb => cb.ThoiGianDi.Date == ThoiGianVe.Date).FirstOrDefault();

                if (chuyenBay == null) return gia;
                gia = gia + (double)chuyenBay.Gia + (double)hangGhe.Gia;
            }
            return gia;
        }
        return 0;
    }

    [WebMethod(EnableSession = true)]
    public string GiuCho(string maSanBayDi, string maSanBayDen, string ngayBay, string ngayVe, List<string> DSLoaiKhachHang, List<string>DSHang)
    {
        if (Session["isLogin"] != null && (bool)Session["isLogin"])
        {
            var NgayBay = DateTime.Parse(ngayBay);
            var NgayVe = DateTime.Parse(ngayVe);
            //List<string> DSLoaiKhachHang = new List<string>();
            //DSLoaiKhachHang.Add("1");
            //DSLoaiKhachHang.Add("1");
            //DSLoaiKhachHang.Add("1");
            //DSLoaiKhachHang.Add("2");
            //DSLoaiKhachHang.Add("2");
            //DSLoaiKhachHang.Add("3");
            //DSLoaiKhachHang.Add("3");
            //DSLoaiKhachHang.Add("3");

            //List<string> DSHang = new List<string>();
            //DSHang.Add("1");
            //DSHang.Add("2");
            //DSHang.Add("3");
            //DSHang.Add("1");
            //DSHang.Add("3");
            //DSHang.Add("2");
            //DSHang.Add("3");
            //DSHang.Add("1");

            if (DSLoaiKhachHang.Count != DSHang.Count)
                return "";
            string DSHangGhe = "";
            string SLHangGhe = "";
            string DSLoaiKH = "";
            string SLLoaiKH = "";

            IEnumerable<string> DistinctDSLKH = DSLoaiKhachHang.Distinct();
            foreach (string LoaiKH in DistinctDSLKH)
            {
                DSLoaiKH = DSLoaiKH + LoaiKH + ",";
                IEnumerable<string> Count = DSLoaiKhachHang.Where(x => x == LoaiKH).ToList();
                SLLoaiKH = SLLoaiKH + Count.Count().ToString() + ",";
            }
            DSLoaiKH = DSLoaiKH.TrimEnd(',');
            SLLoaiKH = SLLoaiKH.TrimEnd(',');

            IEnumerable<string> DistinctDSHG = DSHang.Distinct();
            foreach (string HangGhe in DistinctDSHG)
            {
                DSHangGhe = DSHangGhe + HangGhe + ",";
                IEnumerable<string> Count = DSHang.Where(x => x == HangGhe).ToList();
                SLHangGhe = SLHangGhe + Count.Count().ToString() + ",";
            }
            DSHangGhe = DSHangGhe.TrimEnd(',');
            SLHangGhe = SLHangGhe.TrimEnd(',');

            AirLineEntities entity = new AirLineEntities();
            int MaDatCho = entity.sp_GiuCho(DSHangGhe, SLHangGhe, DSLoaiKH, SLLoaiKH, maSanBayDi, maSanBayDen, NgayBay, NgayVe, 30);
            if (MaDatCho == 0)
                return "";

            return repository.GetAll<DBGiuCho>().LastOrDefault().MaGiuCho.ToString();
        }
        return "";
    }

    [WebMethod(EnableSession = true)]
    public List<ChuyenBay> TimChuyenBayDuocGiuCho(string maGiuCho)
    {
        if (Session["isLogin"] != null && (bool)Session["isLogin"])
        {
            int intMaGiuCho = Convert.ToInt32(maGiuCho);
            List<ChuyenBayGiuCho> chuyenBayGiuChos = repository.Get<ChuyenBayGiuCho>(cbgc => cbgc.MaGiuCho == intMaGiuCho).ToList();
            var MaChuyenBays = chuyenBayGiuChos.Select(x => x.MaChuyenBay).ToArray();
            List<ChuyenBay> chuyenBays = repository.Get<ChuyenBay>(cb => MaChuyenBays.Contains(cb.MaChuyenBay)).ToList();
            return chuyenBays;
        }
        return null;
    }

    [WebMethod(EnableSession = true)]
    public string XacNhanDatVe(string maGiuCho, List<KhachHang> dsHanhKhach, string maChuyenBayDi, string maChuyenBayVe)
    {
        if (Session["isLogin"] != null && (bool)Session["isLogin"])
        {
            int MaGiuCho = Convert.ToInt32(maGiuCho);
            DBGiuCho GiuCho = repository.Get<DBGiuCho>(x => x.MaGiuCho == MaGiuCho).SingleOrDefault();
            if (GiuCho.NgayDat.AddDays(GiuCho.SoNgayGiuCho.Value) < DateTime.Today)
                return "2";
            AirLineEntities entites = new AirLineEntities();
            
            List<CTGiuChoLoaiKhachHang> GCLKH = repository.Get<CTGiuChoLoaiKhachHang>(ctgc => ctgc.MaGiuCho == MaGiuCho).ToList();
            IEnumerable<int> MaLoaiKhachHangs = dsHanhKhach.Select(x => x.LoaiKhachHang).Distinct();
            foreach (int LoaiKH in MaLoaiKhachHangs)
            {
                int SLLoaiKH = dsHanhKhach.Where(x => x.LoaiKhachHang == LoaiKH).ToList().Count;
                if ((int)GCLKH.Where(x => x.MaLoaiKhachHang == LoaiKH && x.SoLuong == SLLoaiKH).ToList().Count == 0)
                    return "0";
            }
            foreach (KhachHang KH in dsHanhKhach)
            {
                try
                {
                    repository.Add<KhachHang>(KH);
                    int MaKhachHang = repository.GetAll<KhachHang>().LastOrDefault().MaKhachHang;
                    int IsThanhCong = entites.spDatVe(MaGiuCho, MaKhachHang, Convert.ToInt32(maChuyenBayDi), Convert.ToInt32(maChuyenBayVe), 1, 1);
                    if (IsThanhCong == 0)
                        return "1";

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return "1";
                }
            }
            int isXacNhan = entites.spXacNhanGiuCho(MaGiuCho, Convert.ToInt32(maChuyenBayDi), Convert.ToInt32(maChuyenBayVe));
            if (isXacNhan == 0)
                return "2";
            return maChuyenBayDi;
        }
        return "";
    }
    [WebMethod(EnableSession = true)]
    public string DatVe(string chuyenBayDi, string chuyenBayVe, List<KhachHang> dsKhachHang, List<string> dsHang)
    {
        if (Session["isLogin"] != null && (bool)Session["isLogin"])
        {
            int MaChuyenBayDi = Convert.ToInt32(chuyenBayDi);
            ChuyenBay ChuyenBayDi = repository.Get<ChuyenBay>(x => x.MaChuyenBay == MaChuyenBayDi).SingleOrDefault();
            ChuyenBay ChuyenBayVe = new ChuyenBay();
            if (string.IsNullOrEmpty(chuyenBayDi) || ChuyenBayDi == null)
                return "0";
            if (dsKhachHang.Count == 0)
                return "3";
            if (dsHang.Count == 0 || dsKhachHang.Count != dsHang.Count)
                return "4";
            if (!string.IsNullOrEmpty(chuyenBayVe))
            {
                int MaChuyenBayVe = Convert.ToInt32(chuyenBayVe);
                ChuyenBayVe = repository.Get<ChuyenBay>(x => x.MaChuyenBay == MaChuyenBayVe).SingleOrDefault();
                if (ChuyenBayVe == null)
                    return "1";
            }
            AirLineEntities entites = new AirLineEntities();
            string DSHangGhe = "";
            string SLHangGhe = "";
            string DSLoaiKH = "";
            string SLLoaiKH = "";
            IEnumerable<int> DistinctDSLKH = dsKhachHang.Select(x => x.LoaiKhachHang).Distinct();
            foreach (int LoaiKH in DistinctDSLKH)
            {
                DSLoaiKH = DSLoaiKH + LoaiKH.ToString() + ",";
                IEnumerable<KhachHang> Count = dsKhachHang.Where(x => x.LoaiKhachHang == LoaiKH).ToList();
                SLLoaiKH = SLLoaiKH + Count.Count().ToString() + ",";
            }
            DSLoaiKH = DSLoaiKH.TrimEnd(',');
            SLLoaiKH = SLLoaiKH.TrimEnd(',');

            IEnumerable<string> DistinctDSHG = dsHang.Distinct();
            foreach (string HangGhe in DistinctDSHG)
            {
                DSHangGhe = DSHangGhe + HangGhe + ",";
                IEnumerable<string> Count = dsHang.Where(x => x == HangGhe).ToList();
                SLHangGhe = SLHangGhe + Count.Count().ToString() + ",";
            }
            DSHangGhe = DSHangGhe.TrimEnd(',');
            SLHangGhe = SLHangGhe.TrimEnd(',');
            int isThanhCong = entites.sp_GiuCho(DSHangGhe, SLHangGhe, DSLoaiKH, SLLoaiKH, ChuyenBayDi.SanBayDi, ChuyenBayVe.SanBayDi, ChuyenBayDi.ThoiGianDi, ChuyenBayVe.ThoiGianDi, 0);
            if (isThanhCong == 0)
            {
                return "2";
            }
            int MaGiuCho = repository.GetAll<DBGiuCho>().LastOrDefault().MaGiuCho;
            foreach (KhachHang KH in dsKhachHang)
            {
                try
                {
                    repository.Add<KhachHang>(KH);
                    int MaKhachHang = repository.GetAll<KhachHang>().LastOrDefault().MaKhachHang;
                    int IsThanhCong = entites.spDatVe(MaGiuCho, MaKhachHang, ChuyenBayDi.MaChuyenBay, ChuyenBayVe.MaChuyenBay, 1, 1);
                    if (IsThanhCong == 0)
                        return "1";

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return "1";
                }
            }
            return "0";
        }
        return "0";
    }
    [WebMethod(EnableSession = true)]
    public List<Ve> XemThongTinVeTuMaDat(string maDatVe)
    {
        if (Session["isLogin"] != null && (bool)Session["isLogin"])
        {
            int MaDatVe = Convert.ToInt32(maDatVe);
            List<Ve> Ves = repository.Get<Ve>(x => x.MaGiuCho == MaDatVe).ToList();
            return Ves;
        }
        return null;
    }
    [WebMethod(EnableSession = true)]
    public KhachHang XemThongTinKhachHangTuVe(string maDatVe)
    {
        if (Session["isLogin"] != null && (bool)Session["isLogin"])
        {
            int MaDatVe = Convert.ToInt32(maDatVe);
            Ve Ve = repository.Get<Ve>(x => x.MaGiuCho == MaDatVe).FirstOrDefault();
            KhachHang khachHang = repository.Get<KhachHang>(x => x.MaKhachHang == Ve.MaKhachHang).SingleOrDefault();
            return khachHang;
        }
        return null;
    }

}
